package org.ectar.papi
{
	import flash.xml.XMLNode;
	import flash.xml.XMLNodeType;
	
	import org.ectar.papi.data.GAlbum;
	import org.ectar.papi.data.GEntry;
	
	/**
	 * Class contains a bunch of utility methods for creating XML Atom feed to describe
	 * various Google Data API etntities
	 * 
	 * @author Misha Lytvynyuk, May 2009
	 */
	internal class GDataGenerator	{

		private namespace media = "http://search.yahoo.com/mrss/" ;
        use namespace media;
        
        private namespace gphoto = "http://schemas.google.com/photos/2007" ;
        use namespace gphoto;
        
        default xml namespace = "http://www.w3.org/2005/Atom";
        
		public function GDataGenerator() {}
		
		/**
		 * Internal function
		 * 
		 * Create template XML, them fill the data into appropriate elements using E4x.
		 * E4x rocks for those kind of purpuses. saving much time.
		 * 
		 * @param albumDescriptor - instance of GAlbum, but only requited fields are populated.
		 * 				<code>*title, summary, 
		 * 				gphoto.location, *gphoto.access , *gphoto.commentingEnabled, *gphoto.timestamp
		 * 				media.keywords
		 * 				category</code>
		 * '*' - means mandatory fields, functionn doesnt do any validatio.
		 * TODO: implement validation.
		 * 
		 * @return XML string
		 * 
		 */
		internal static function creatAlbumAtomDescriptorXML(albumDescriptor:GAlbum):String {
			var xml:XML = 	<entry
								xmlns='http://www.w3.org/2005/Atom'
								xmlns:media='http://search.yahoo.com/mrss/'
    							xmlns:gphoto='http://schemas.google.com/photos/2007'>
    								<title type='text'></title>
	  								<summary type='text'></summary>
	  								<gphoto:location></gphoto:location>
									<gphoto:access></gphoto:access>
									<gphoto:commentingEnabled></gphoto:commentingEnabled>
									<gphoto:timestamp></gphoto:timestamp>
									<media:group>
										<media:keywords></media:keywords>
									</media:group>
									<category scheme='http://schemas.google.com/g/2005#kind'
										term='http://schemas.google.com/photos/2007#album'></category>
							</entry>
			 
			xml.title.text()[0] = albumDescriptor.title?albumDescriptor.title:"";

			xml.summary.text()[0] = albumDescriptor.summary?albumDescriptor.summary:"";

			xml.gphoto::location = albumDescriptor.gphoto.location?albumDescriptor.gphoto.location:"";
			xml.gphoto::access = albumDescriptor.gphoto.access?albumDescriptor.gphoto.access:"";
			xml.gphoto::commentingEnabled = albumDescriptor.gphoto.commentingEnabled?albumDescriptor.gphoto.commentingEnabled:"";
			xml.gphoto::timestamp = albumDescriptor.gphoto.timestamp?albumDescriptor.gphoto.timestamp:"";
			
			xml.media::group.media::keywords = albumDescriptor.media.keywords?albumDescriptor.media.keywords:"";
			
			xml.category.text()[0] = albumDescriptor.category?albumDescriptor.category:"";
			
			return xml.toString();
		}
		
		/**
		 * TODO: implement
		 * 
		 * @param photoDescroptor
		 * @return 
		 * 
		 */
		internal static function creatPhotoAtomDescriptorXML(photoDescroptor:GEntry):String{
			return "";
		}

		/**
		 * Creates XML element containing all required child nodes to create new comment.
		 * 
		 * @param commentDescriptor - text of the comment
		 * @return - string representation of XML element.
		 * 
		 */
		internal static function creatCommentAtomDescriptorXML(commentDescriptor:String):String {
				var entry_node:XMLNode = new XMLNode(XMLNodeType.ELEMENT_NODE,"entry");
				entry_node.attributes.xmlns = "http://www.w3.org/2005/Atom";

				var content_node:XMLNode = new XMLNode(XMLNodeType.ELEMENT_NODE,"content");
				var comment_text_node:XMLNode = new XMLNode(XMLNodeType.TEXT_NODE,"");
				comment_text_node.nodeValue = commentDescriptor;
				content_node.appendChild(comment_text_node);
				
				var category_node:XMLNode = new XMLNode(XMLNodeType.ELEMENT_NODE,"category ");
				category_node.attributes.scheme = "http://schemas.google.com/g/2005#kind";
				category_node.attributes.term = "http://schemas.google.com/photos/2007#comment";
				
				entry_node.appendChild(content_node);
				entry_node.appendChild(category_node);
				
				trace(entry_node.toString());
				
				return entry_node.toString();
		}
	}
}